import { Component,OnInit } from '@angular/core';
import { Router }  from '@angular/router';
import { HttpRequest } from "./service/httpRequest";
import { TranslateService } from 'ng2-translate';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})

export class AppComponent implements OnInit {
  constructor(private router: Router,
              public translate: TranslateService){

  }


  ngOnInit(){
    this.translate.addLangs(["zh", "en"]);
    this.translate.setDefaultLang('zh');

    const browserLang = this.translate.getBrowserLang();

    if(localStorage.getItem("language")){
      this.translate.use(localStorage.getItem("language"));
    }
    else{
      this.translate.use(browserLang.match(/zh|en/) ? browserLang : 'zh');
      localStorage.setItem("language",browserLang.match(/zh|en/) ? browserLang : 'zh');
    }

    if(!(localStorage.getItem("isLogin"))) {
      this.router.navigateByUrl('/login');
      return ;
    }
    console.log("检测到的浏览器语言>"+this.translate.currentLang);
  }

}
